﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace JY.Wpf.Controls.ControlHelpers
{
    public class AdornerContainer : Adorner
    {
        private UIElement _child;

        public AdornerContainer(UIElement adornedElement) : base(adornedElement)
        {
        }

        public UIElement Child
        {
            get { return this._child; }
            set
            {
                if (value == null)
                {
                    RemoveVisualChild(this._child);
                    this._child = value;
                    return;
                }
                AddVisualChild(value);
                this._child = value;
            }
        }

        protected override int VisualChildrenCount
        {
            get { return this._child != null ? 1 : 0; }
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            this._child?.Arrange(new Rect(finalSize));
            return finalSize;
        }

        protected override Visual GetVisualChild(int index)
        {
            if (index == 0 && this._child != null) return this._child;
            return base.GetVisualChild(index);
        }
    }
}
